#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{
  pid_t id = fork();//创建子进程
  if(id == 0)//child
  {
 //  char *const myargv[] = {
 //     "ls",
 //     "-l",
 //     "-a",
 //     NULL
 //   };
     printf("before:I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
    //进程替换五种方式
    
   // execl("/usr/bin/ls","ls","-a","-l",NULL);//这是以列表的方式
   // execlp("ls","ls","-a","-l",NULL);
   // execv("/usr/bin/ls",myargv);
   // execvp("ls",myargv);
    //让子进程替换另一个程序
     char *const myargv[] = {
       "otherExe",
       "-a",
       "-b",
       "-c",
       NULL
     };
     char *const myenv[] = {
       "MYVAL = 1111",
       "MYPATH=/usr/bin/XXX",
       NULL
     };
    // execv("./otherExe",myargv);
      execle("./otherExe","otherExe","-a","-w","-v",NULL,myenv);

     printf("after:I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
     exit(1);//如果子进程执行完了，让他退出，避免执行多余程序
  }
  //father
  pid_t ret = waitpid(id,NULL,0);//进程等待
  if(ret > 0) printf("wait success,father pid:%d,ret id:%d\n",getpid(),getppid());
  sleep(5);//等五秒终止进程
return 0;
}
